#include "SockStream.h"
#include "SockAcceptor.h"

#include <errno.h>
#include <stdio.h>
#include <sys/epoll.h>
#include <iostream>

int main ()
{
    InetAddr local_addr (9999);

    SockStream peer;
    SockAcceptor acceptor;
    if (acceptor.open (local_addr) != 0)
    {
	std::cout << errno << std::endl;
	perror ("open");
	return 0;
    }
    InetAddr remote_addr;
    if (acceptor.accept (peer, &remote_addr) != 0)
    {
	std::cout << errno << std::endl;
	perror ("accept");
	return 0;
    }
    char buff[512] = {0};
#if 0
    while (1)
    {
	int len = peer.recv (buff, 512);
	if (len <= 0)
	    break;
	std::cout << buff << std::endl;
	peer.send (buff, len);
    }
#elif 0
    int ret = Socket::handle_read_ready (peer.handle (), 2*1000);
    if (ret <= 0)
    {
	perror ("select");
	std::cout << "timeout of error, ret = " << ret << std::endl;
    }
    peer.recv (buff, 512);
    std::cout << buff << std::endl;
    std::cout << "start to sleep, wait client close\n";
    sleep (5);
    ret = Socket::handle_read_ready (peer.handle (), 2*1000);
    if (ret <= 0)
    {
	perror ("select2");
	std::cout << "timeout of error 2, ret = " << ret << std::endl;
    }
    int len = peer.recv (buff, 512);
    if (len == 0)
    {
	std::cout << "client close\n";
    }
#   else
    int ep_fd = epoll_create (20);
    struct epoll_event ep, evs[20];
    ep.events  = EPOLLIN;
    ep.data.fd = peer.handle ();
    epoll_ctl (ep_fd, EPOLL_CTL_ADD, peer.handle (), &ep);

    int ret = epoll_wait (ep_fd, evs, 20, 2*1000);
    if (ret <= 0)
    {
	perror ("select");
	std::cout << "timeout of error, ret = " << ret << std::endl;
    }
    peer.recv (buff, 512);
    std::cout << buff << std::endl;
    std::cout << "start to sleep, wait client close\n";
    sleep (5);
    ret = epoll_wait (ep_fd, evs, 20, 2*1000);
    if (ret <= 0)
    {
	perror ("select2");
	std::cout << "timeout of error 2, ret = " << ret << std::endl;
    }
    int len = peer.recv (buff, 512);
    if (len == 0)
    {
	std::cout << "client close\n";
    }

#endif
    peer.close ();
    return 0;
}
